from sys import exc_info
from muMod import *
from win32.win32gui import FindWindow, SetWindowPos
wins = []

title = "永恒之巅"
#hwnd = FindWindow(None, title)
hwnd = 721750
SetWindowPos(hwnd, -2, 1942, 0, 864, 530, 1) #窗口置顶 736, 332；2016, 456
sleep(1)
rect = RECT()
if hwnd:
	windll.user32.GetWindowRect(hwnd, byref(rect))

window={"hWnd":hwnd, "rect":rect, "title":title, "map":"vip", "mode":"vip", "bound":[],
	"coords":[], "last_coord":[0.6, 0.51],
	"black_coords":[],
	"black_len": 0, "last_time":time(), "tsleep":[], "line":2,"turn":0, "hour":24}
window["black_len"] = len(window["black_coords"])

def vip_Play(window, monitor = 0, top = False, fen = False):
	n = 0; fen_count=0 #循环外初始化
	while True:
		rect = window["rect"]
		hWnd = window["hWnd"]
		diagClose(hWnd, rect, monitor, window["map"], window["line"], top) # 检测界面异常
		if len(window["black_coords"]) > window["black_len"] and (time() - window["last_time"] > 120):
			del window["black_coords"][window["black_len"]] # 移出坐标黑名单
		act_stat = handAct(hWnd, rect,  monitor, top)
		if not act_stat[0]:
			sleep(5) # 捡东西
			map_result = None
			while map_result is None or map_result["confidence"]<0.7:
				close_all(rect, monitor, hWnd)
				bkg_click(hWnd,coords_spec["map"]) # 打开地图
				sleep(0.2)
				bkg_click(hWnd, (760,340)); sleep(0.3)
				bkg_click(hWnd, (760,340)); sleep(0.7)  # 放大
				image = loc_capture(hWnd, rect, monitor) # 截图
				map_result = find_template(image[90:107, 146:180], map_img)
			sleep(2.5)
			player_des = getPlayer(image)
			if not player_des: # 未获取玩家坐标
				player_des = {"result":window["last_coord"]} # 用上次坐标代替玩家坐标
			if 5 < localtime(time()).tm_hour < 11:
				window["coords"] = bossls(player_des["result"], image, window["black_coords"], king_only = True)
			else:
				window["coords"] = bossls(player_des["result"], image, window["black_coords"])
			if type(window["coords"]) is bool:
				bkg_click(hWnd, (102,97)) # 关闭地图
				continue
			bkg_click(hWnd, window["coords"][0][0]) # 点击最近目标
			ts = int(2 * np.sqrt(window["coords"][0][1]))-1 if window["coords"][0][1] >10 else int(2 * np.sqrt(window["coords"][0][1]))
			sleep(ts) # 2 * 距离平方根
			#close_all(rect, monitor, hWnd)
			bkg_click(hWnd, (102,97)) # 关闭地图
			bkg_click(hWnd, coords_spec["hand"]); sleep(2)  # 点击手动
			window["last_coord"] = list(window["coords"][0][0]) # 更新上次选择坐标
			sleep(2)
			act_stat = handAct(hWnd, rect,  monitor, top)
			if not act_stat[0]:	# 没到呢, 继续走
				bkg_click(hWnd, coords_spec["map"]) # 打开地图
				bkg_click(hWnd, (760,340)); sleep(0.3)
				bkg_click(hWnd, (760,340)); sleep(0.7)  # 放大
				bkg_click(hWnd, window["coords"][0][0], int(2 * np.sqrt(window["coords"][0][1]))) # 继续之前目标
				sleep(5)
				#close_all(rect, monitor, hWnd)
				bkg_click(hWnd, (102,97)) # 关闭地图
				bkg_click(hWnd, coords_spec["hand"], 2)  # 点击手动
		elif act_stat[0] and not act_stat[2]:
			bkg_click(hWnd, coords_spec["hand"])  # 点击手动
		elif act_stat[0] and not act_stat[1]: # 抢归属
			 window["black_coords"] += window["coords"][0][0]
			 window["last_time"] = time()
			 continue
		else:	#降低回收操作频率
			n = n+1 if n<20 else 0
			if n == 20:
				# 回收
				bkg_click(hWnd, coords_spec["pack"]); sleep(3)
				bkg_click(hWnd, (815,485)); sleep(2) # 整理
				bkg_click(hWnd, coords_spec["recycle1"]); sleep(3)
				bkg_click(hWnd, coords_spec["recycle2"]); sleep(3)
				#close_all(rect, monitor, hWnd)
				bkg_click(hWnd, (609,62))
				fen_count +=1 #回收5次分解1次
				#sleep(tsleep)
			if fen and fen_count == 5: # 分解
				fen_count = 0
				bkg_click(hWnd, (850,168)); sleep(1)
				bkg_click(hWnd, (850,316)); sleep(2)	#锻造
				bkg_click(hWnd, (850,300)); sleep(1)	#分解
				for fx in range(350,530,55):
					bkg_click(hWnd, (fx, 190))
					sleep(0.5)
				bkg_click(hWnd, (700,490)); sleep(1)	#分解
				bkg_click(hWnd, (565,62))
		sleep(3)

#try:
vip_Play(window, 1440, True, True)
#except:
#	print("Unexpected error:", exc_info()[0:2])
#	pass

# 遗址1, 在距离最近的两个黄金怪之间来回刷, 红怪出现时也刷红(除了地图左边那个变态红)
window={"hWnd":hwnd, "rect":rect, "title":title, "map":"yivi1", "mode":"king",
		"bound":[2,3,1,0], "coords":[(337,177), (575,361), (552,316), (587,267), (504,221)],
		"last_coord":[0.6, 0.51], "last_time":time() - 45, "tsleep":[26,6.5,5,8.5], "line":2, "turn":0, "hour":24}

def yivi1_Play(window, monitor = 0, top = False):
	from sys import exc_info
	#print(window["title"], window["map"])
	try:
		n = 0
		while True:
			rect = window["rect"]
			hWnd = window["hWnd"]
			diagClose(hWnd, rect, monitor, window["map"], window["line"], top) # 检测界面异常
			act_stat = handAct(hWnd, rect,  monitor, top)
#			SetWindowPos(hWnd, 1, 0, 0, 0, 0, 2 | 1) # 下沉窗口
			if not act_stat[0]:
				map_result = None
				while map_result is None or map_result["confidence"]<0.7:
					close_all(rect, monitor, hWnd)
					bkg_click(hWnd, (770,110)) # 打开地图
					sleep(0.7)
					image = loc_capture(hWnd, rect, monitor, top)
					map_result = find_template(image[90:107, 146:180], map_img) # 地图
				sleep(2.5)
				match_result = find_template(image[209:245,470:497], boss_alive)
				#print("Before:", window["bound"], window["turn"])
				if match_result is not None and match_result["confidence"] > 0.7 and window["bound"][3] == 0:
					window["bound"][:2] = [1,4] # 坐标序列上下限
					window["bound"][3] = 2 # 设置Boss数量
				elif (match_result is None or match_result["confidence"] < 0.7) and window["bound"][3] == 0:
					window["bound"][:2] = [2,3]
				player_des = getPlayer(image)
				if player_des: #根据距离判断序号
					min_dis = 60
					for i in range(len(window["coords"])):
						dis = np.linalg.norm(np.array(window["coords"][i]) - np.array(player_des["result"]), ord=1)
						if dis < min_dis:
							min_dis = dis
							window["turn"] = i
				if window["turn"] == 0:
					window["turn"] = 2
					window["bound"][2] = 1
					bkg_click(hWnd, window["coords"][2])
					sleep(29)
#					bkg_click(hWnd, (102,97)) # 关闭地图
				elif not window["bound"][0] <= window["turn"] + window["bound"][2] <= window["bound"][1]:
					window["bound"][2] = window["bound"][2]*(-1) #切换方向
				if time() - window["last_time"] > 45: # 换点间隔60s
					window["turn"] = window["turn"] + window["bound"][2]
					tsleep = window["tsleep"][window["turn"]-1] if window["bound"][2] == 1 else window["tsleep"][window["turn"]]
					bkg_click(hWnd, window["coords"][window["turn"]])
#					print("turn:",window["turn"], "ts:", tsleep, "bound:", window["bound"])
					sleep(tsleep)
					window["last_coord"] = window["coords"][window["turn"]]
					window["last_time"] = time()
					#close_all(rect, monitor, hWnd)
					bkg_click(hWnd, (102,97)) # 关闭地图
				else:
					bkg_click(hWnd, (102,97)) # 关闭地图
					sleep(10)
					continue
				# 开干
				bkg_click(hWnd, coords_spec["hand"])  # 点击手动
				sleep(2)
				if window["bound"][3] > 0 and window["turn"] in [1,4]: window["bound"][3] -= 1
			elif act_stat[0] and not act_stat[2]:
				bkg_click(hWnd, coords_spec["hand"])  # 点击手动
			else:	#降低回收操作频率
				n = n+1 if n<30 else 0
				if n == 30:
					# 回收
					bkg_click(hWnd, coords_spec["pack"]); sleep(2)
					bkg_click(hWnd, (815,485)); sleep(1) # 整理
					bkg_click(hWnd, coords_spec["recycle1"]); sleep(2)
					bkg_click(hWnd, coords_spec["recycle2"]); sleep(2)
					#close_all(rect, monitor, hWnd)
					bkg_click(hWnd, (609,62))
					#sleep(tsleep)
			sleep(5)
	except:
		print("Unexpected error:", exc_info()[0:2])
		pass
